package com.system.healthproject.model.doctor.controller;

import com.system.healthproject.annotation.RequireToken;
import com.system.healthproject.bean.PagesBean;
import com.system.healthproject.bean.ParamDoctorRegister;
import com.system.healthproject.bean.ResultDoctorConsulting;
import com.system.healthproject.bean.web.*;
import com.system.healthproject.common.ApiResult;
import com.system.healthproject.model.admin.paramsbean.*;
import com.system.healthproject.model.doctor.params.DoctorAddRecordParams;
import com.system.healthproject.model.doctor.params.DoctorNumberParams;
import com.system.healthproject.model.doctor.params.FollowPatientParams;
import com.system.healthproject.model.doctor.service.DoctorService;
import com.system.healthproject.model.sickuser.params.UserPatientInfo;
import com.system.healthproject.utils.RedisUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "医生端-用户",tags = "医生端-用户管理API")
@RestController
@RequestMapping("/doctor/user")
public class DoctorController {

    @Autowired
    DoctorService doctorService;
    @Autowired
    RedisUtil redisUtil;

    @ApiOperation(value = "医生用户登录接口(后台已调)",notes = "验证医生用户的用户名和密码",httpMethod = "post")
    @PostMapping(value = "/login",produces = "application/json;charset=utf-8")
    public ApiResult<String> login(@RequestBody
                                   @ApiParam(name = "user",value = "医生用户名和密码",required = true) LoginParams loginParams){
        String token = doctorService.login(loginParams);
        if(token!=null){
            return ApiResult.successWithData("成功",token);
        }else {
            return ApiResult.failure("登录失败,请重新登录");
        }
    }

    @RequestMapping(value = "/addDoctor" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    @ApiOperation(value = "医生注册(后台已调用)",notes = "医生注册",httpMethod = "post",response = ApiResult.class)
    @ResponseBody
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "电话号码",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "verificationCode",value = "短信验证码",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "password",value = "密码",paramType = "query",dataType = "String",required = true),
    })
    public ApiResult addDoctor(ParamDoctorRegister paramDoctorRegister){
        String token = doctorService.registeDoctor(paramDoctorRegister);
        if(token!=null){
            return ApiResult.successWithData(token);
        }else {
            return ApiResult.failure("注册失败");
        }
    }

    /**
     * 医生查看个人信息
     */
    @RequireToken
    @ApiOperation(value = "查询医生的个人信息(后台已调)",notes = "验证医生用户的用户名和密码",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorInfo" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult selectDoctorInfo(@RequestHeader("token")String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        ResultDoctorInfo doctorInfo = doctorService.selectDoctorInfo(loginId);
        return ApiResult.successWithData("成功",doctorInfo);
    }


    /**
     * 医生修改个人信息
     */
    @RequireToken
    @ApiOperation(value = "修改医生的个人信息(后台已调)",notes = "验证医生用户的用户名和密码",httpMethod = "post")
    @RequestMapping(value = "/updateDoctorInfo" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult updateDoctorInfo(@RequestHeader("token")String token, @RequestBody@ApiParam("医生修改个人信息参数")UpdateDoctorInfo updateDoctorInfo){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        String msg = doctorService.updateDoctorInfo(loginId,updateDoctorInfo);
        return ApiResult.successWithData("成功",msg);
    }
    /**
     * 医生查看诊疗活动
     */
    @RequireToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "date",value = "日期（yyyy-MM-dd）",paramType = "query",dataType = "String",required = true),
    })
    @ApiOperation(value = "医生查看诊疗活动(后台已调)",notes = "医生查看诊疗活动",httpMethod = "post")
    @RequestMapping(value = "/selectDayConsulting" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult selectDayConsulting(@RequestHeader("token")String token,String date){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        List<ResultDoctorConsulting> list =  doctorService.selectDayConsulting(loginId,date);
        return ApiResult.successWithData("成功",list);
    }

    /**
     * 创建诊疗活动--提醒人群--分组
     */
    @RequireToken
    @ApiOperation(value = "创建诊疗活动--提醒人群--分组(接口已调用) ",notes = "查看医生分组信息",httpMethod = "post")
    @RequestMapping(value = "/selectDoctorGruop" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult selectDoctorGruop(@RequestHeader("token")String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        List<ResultDoctorGroup> list = doctorService.selectDoctorGroup(loginId);
        return ApiResult.successWithData("成功",list);
    }
    /**
     * 创建诊疗活动--提醒人群--全部
     */
    @RequireToken
    @ApiOperation(value = "创建诊疗活动--提醒人群--全部(提醒人群)接口已调用 ",notes = "查看医生分组信息",httpMethod = "post")
    @RequestMapping(value = "/selectAllPatient" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult< List<ResultAllPatient>> selectAllPatient(@RequestHeader("token")String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        List<ResultAllPatient> list = doctorService.selectAllPatient(loginId);
        return ApiResult.successWithData("成功",list);
    }


    /**
     * 医生添加诊疗活动
     */
    @RequireToken
    @ApiOperation(value = "医生新增诊疗活动(接口已调用)",notes = "医生新增诊疗活动",httpMethod = "post")
    @RequestMapping(value = "/addConsulting" ,method = RequestMethod.POST,produces = "application/json;charset=utf-8")
    public ApiResult addConsulting(@RequestHeader("token")String token,
                                   @RequestBody ParamAddConsulting addConsulting){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
       String msg =  doctorService.addConsulting(loginId,addConsulting);
        return ApiResult.success(msg);
    }

    /**
     * 医生首页 患者数量参数获取
     * @param token
     * @return
     */
    @RequireToken
    @ApiOperation(value = "医生首页获取患者数量(后端已调)",httpMethod = "get")
    @GetMapping(value = "/doctorGetNumber",produces = "application/json;charset=utf-8")
    public ApiResult<DoctorNumberParams> doctorGetNumber(
            @RequestHeader("token")String token){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");

        DoctorNumberParams doctorNumberParams = doctorService.doctorGetNumber(loginId);
        return ApiResult.successWithData("成功",doctorNumberParams);
    }


    @RequireToken
    @ApiImplicitParam(name = "provinceCode",value = "省份识别码",paramType = "query",dataType = "string",required = true)
    @ApiOperation(value = "医生根据省份获取关联患者数量(后端已调)",httpMethod = "get")
    @GetMapping(value = "/doctorGetNumberByProvinceCode",
            produces = "application/json;charset=utf-8")
    public ApiResult<DoctorNumberParams> doctorGetNumberByProvinceCode(
            @RequestHeader("token")String token,
             Integer provinceCode){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");

        DoctorNumberParams patientNumberParams =
                doctorService.doctorGetNumberByProvinceCode(loginId,provinceCode);

        return ApiResult.successWithData("成功",patientNumberParams);
    }

    @RequireToken
    @ApiOperation(value = "后端获取关联患者列表(后端已调)",httpMethod = "get")
    @GetMapping(value = "/doctorGetFollowList",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<FollowPatientParams>> doctorGetFollowList(
            @RequestHeader("token")String token,
            Integer page, Integer size
    ){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
        PagesBean<FollowPatientParams> followPatientParamsPagesBean = doctorService.doctorGetFollowList(page, size, loginId);
        return ApiResult.successWithData(followPatientParamsPagesBean);
    }


    @RequireToken
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "size",value = "每页数量",paramType = "query",dataType = "int",required = true)
    })
    @ApiOperation(value = "获取患者的个人就诊记录(后端已调)",httpMethod = "get")
    @GetMapping(value = "/visitRecordsList",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<VisitRecordsShowParams>> getVisitRecordsList(
            int page,
            int size,
            int userId,
            @RequestHeader("token")String token
    ){
        PagesBean<VisitRecordsShowParams> visitRecordsList
                =
                doctorService.getVisitRecordsList(page, size, userId);
        return ApiResult.successWithData("成功",visitRecordsList);
    }

    @RequireToken
    @ApiOperation(value = "获取患者发作次数(后台已调)",notes = "患者获取发作次数",httpMethod = "get")
    @GetMapping(value = "/userGetFrequencyList",produces = "application/json;charset=utf-8")
    public ApiResult<List<FrequencyParams>>  userGetFrequencyList(
            Integer patientId,int type,
            @RequestHeader("token")String token){


        List<FrequencyParams> frequencyParams = doctorService.userGetFrequencyList(patientId,type);
        return ApiResult.successWithData(frequencyParams);
    }

    @RequireToken
    @ApiOperation(value = "获取患者详情(后端已调)",notes = "获取患者详情",httpMethod = "get")
    @GetMapping(value = "/getPatientInfo",produces = "application/json;charset=utf-8")
    public ApiResult<UserPatientInfo> getPatientInfo(
            Integer patientId,
            @RequestHeader("token")String token){
        UserPatientInfo patientInfo = doctorService.getPatientInfo(patientId);
        return ApiResult.successWithData(patientInfo);
    }

    @RequireToken
    @ApiOperation(value = "获取患者症状列表(后台已调)",notes = "患者获取症状列表",httpMethod = "get")
    @GetMapping(value = "/userGetReactions",produces = "application/json;charset=utf-8")
    public ApiResult<List<ReactionsParams> >   userGetReactions(
            Integer patientId,
            int type,
            @RequestHeader("token")String token){
        List<ReactionsParams> reactionsParams = doctorService.userGetReactions(patientId,type);
        return ApiResult.successWithData(reactionsParams);
    }

    @RequireToken
    @ApiOperation(value = "患者获取病例详情(后台已调)",notes = "患者获取病例详情",httpMethod = "get")
    @GetMapping(value = "/userGetRecordInfo",produces = "application/json;charset=utf-8")
    public ApiResult<RecordInfoParams> userGetRecordInfo(
            Integer patientId,
            int recordId,
            @RequestHeader("token")String token){
        RecordInfoParams recordInfoParams = doctorService.userGetRecordInfo(patientId, recordId);
        return ApiResult.successWithData(recordInfoParams);
    }

    @RequireToken
    @ApiOperation(value = "医生为患者为就诊记录添加病例",notes = "医生为患者添加病例",httpMethod = "get")
    @PostMapping(value = "/addRecordWithVistRecord",produces = "application/json;charset=utf-8")
    public ApiResult addRecordWithVistRecord(
            @RequestBody DoctorAddRecordParams params,
            @RequestHeader("token")String token
            ){
        ApiResult objectApiResult = doctorService.addRecordWithVistRecord(params);
        return objectApiResult;
    }

    @RequireToken
    @ApiOperation(value = "医生获取关联医生的男女数量",httpMethod = "get")
    @GetMapping(value = "/getSexNumber",produces = "application/json;charset=utf-8")
    public ApiResult<PagesBean<FollowPatientParams>> getSexNumber(
            @RequestHeader("token")String token
    ){
        Map<String, Object> map = (Map<String, Object>) redisUtil.get(token);
        Integer loginId = (Integer) map.get("loginId");
      //  doctorService.getSexNumber(loginId);
        return null;
    }


}
